//
//  DiagnosisResultViewController.m
//  MobileChecker
//
//  Created by Lymons on 15-1-12.
//  Copyright (c) 2015年 Lymons. All rights reserved.
//

#import "DiagnosisResultViewController.h"
#import "DiagnosisListViewController.h"
#import "CheckResultItemViewModel.h"
#import "LayoutUtils.h"

@interface DiagnosisResultViewController ()

@property (strong, nonatomic) IBOutlet UIButton *returnToFirstButton;
@property (strong, nonatomic) IBOutlet UIButton *againButton;
@property (strong, nonatomic) IBOutlet UIButton *completedButton;

@end

@implementation DiagnosisResultViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.returnToFirstButton.hidden = self.hideBottomButton;
    self.againButton.hidden = self.hideBottomButton;
    self.completedButton.hidden = self.hideBottomButton;
    if (self.hideBottomButton) {
        [self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *obj, NSUInteger idx, BOOL *stop) {
            if (obj.firstItem == self.againButton && obj.secondItem == self.tableView) {
                [self.view removeConstraint:obj];
                *stop = YES;
                NSLog(@"%@", obj);
            }
        }];
        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView
                                  attribute:NSLayoutAttributeBottom
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:self.view
                                  attribute:NSLayoutAttributeBottom
                                  multiplier:1
                                  constant:-20]];
    }
    if ([self.viewModel isKindOfClass:[CheckResultItemViewModel class]]) {
        [(CheckResultItemViewModel *)(self.viewModel) updateResult];
        [self.tableView reloadData];
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (!self.hideBottomButton) {
        [self.navigationController setViewControllers:@[self]];
    }
}

- (void)bindViewModel
{
    self.viewModel = [[CheckResultItemViewModel alloc] init];
    self.tableViewCellIdentifier = @"DiagnosisResultTableViewCell";
    
    [super bindViewModel];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"CheckAgain"]) {
        if ([segue.destinationViewController isKindOfClass:[DiagnosisListViewController class]]) {
            DiagnosisListViewController *vc = (DiagnosisListViewController *)segue.destinationViewController;
            vc.needToCheckFromBegining = YES;
        }
    }
}

@end
